package com.sap.sap_mangers.controller;

import com.sap.sap_mangers.dto.luckyDraw.Price;
import com.sap.sap_mangers.pojo.SapLuckyDrawUser;
import com.sap.sap_mangers.service.OtherService;
import com.sap.sap_mangers.until.RedisCache;
import com.sap.sap_mangers.until.SimpleJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.sap.sap_mangers.until.ConstantUntil.OPEN_THE_LUCKY_DRAW;

/**
 * @author 阿东
 * @date 2022/8/30 [11:16]
 * 这个用来处理一些杂事(比如调用其他接口，或者生成某个前端的句子之类的)
 */
@RestController
public class OtherController {


    @Autowired
    private OtherService otherService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取随机一条朋友圈文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getPyqCopyWriting")
    public SimpleJson getPyqCopyWriting() {
        return new SimpleJson(otherService.getPyqCopyWriting());
    }

    ;

    /**
     * 获取随机一条电影台词文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getDytcCopyWriting")
    public SimpleJson getDytcCopyWriting() {
        return new SimpleJson(otherService.getDytcCopyWriting());
    }

    ;

    /**
     * 获取随机一条网易云热评文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getWyyCopyWriting")
    public SimpleJson getWyyCopyWriting() {
        return new SimpleJson(otherService.getWyyCopyWriting());
    }

    ;

    /**
     * 获取随机一条早安心语文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getZaxyCopyWriting")
    public SimpleJson getZaxyCopyWriting() {
        return new SimpleJson(otherService.getZaxyCopyWriting());
    }

    ;

    /**
     * 获取随机一条晚安心语文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getWaxyCopyWriting")
    public SimpleJson getWaxyCopyWriting() {
        return new SimpleJson(otherService.getWaxyCopyWriting());
    }

    ;

    /**
     * 获取随机一条毒鸡汤文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getDjtCopyWriting")
    public SimpleJson getDjtCopyWriting() {
        return new SimpleJson(otherService.getDjtCopyWriting());
    }

    ;

    /**
     * 获取全部朋友圈文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getPyqCopyWritingSum")
    public SimpleJson getPyqCopyWritingSum() {
        return new SimpleJson(otherService.getPyqCopyWritingSum());
    }

    ;

    /**
     * 获取全部电影台词文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getDytcCopyWritingSum")
    public SimpleJson getDytcCopyWritingSum() {
        return new SimpleJson(otherService.getDytcCopyWritingSum());
    }

    ;

    /**
     * 获取全部网易云热评文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getWyyCopyWritingSum")
    public SimpleJson getWyyCopyWritingSum() {
        return new SimpleJson(otherService.getWyyCopyWritingSum());
    }

    ;

    /**
     * 获取全部早安心语文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getZaxyCopyWritingSum")
    public SimpleJson getZaxyCopyWritingSum() {
        return new SimpleJson(otherService.getZaxyCopyWritingSum());
    }

    ;

    /**
     * 获取全部晚安心语文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getWaxyCopyWritingSum")
    public SimpleJson getWaxyCopyWritingSum() {
        return new SimpleJson(otherService.getWaxyCopyWritingSum());
    }

    ;

    /**
     * 获取全部毒鸡汤文案
     *
     * @return
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getDjtCopyWritingSum")
    public SimpleJson getDjtCopyWritingSum() {
        return new SimpleJson(otherService.getDjtCopyWritingSum());
    }

    @PreAuthorize("hasAuthority('会长')")
    @GetMapping("/openTheDraw")
    public SimpleJson openTheDraw(){
        String luckyState = redisCache.getCacheObject(OPEN_THE_LUCKY_DRAW);
        if(luckyState==null){
            redisCache.setCacheObject(OPEN_THE_LUCKY_DRAW,"是");
            return new SimpleJson(0,"抽奖入口开启成功");
        }
        if("是".equals(luckyState)){
            redisCache.setCacheObject(OPEN_THE_LUCKY_DRAW,"否");
            return new SimpleJson(1,"抽奖入口关闭成功");
        }else{
            redisCache.setCacheObject(OPEN_THE_LUCKY_DRAW,"是");
            return new SimpleJson(0,"抽奖入口开启成功");
        }
    }

    @PreAuthorize("hasAuthority('会长')")
    @GetMapping("/createLuckyDrawTheme")
    public SimpleJson createLuckyDrawTheme(@RequestParam("context")String context){
        try{
            otherService.createLuckyDrawTheme(context);
        }catch (Exception e){
            e.printStackTrace();
            return new SimpleJson(1,"出现未知问题，请排查服务器日志");
        }
        return new SimpleJson(0,"最新的抽奖活动创建成功");
    }

    @PreAuthorize("hasAuthority('会长')")
    @PostMapping("/createLuckyDrawPrice")
    public SimpleJson createLuckyDrawPrice(HttpServletRequest request){
        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
        List<MultipartFile> image = ((MultipartHttpServletRequest) request).getFiles("image");
        if (image.size()!=1) {
            return new SimpleJson(1,"有且只能有一张图片");
        }
        Price price = new Price();
        price.setImages(image);
        price.setPrice(params.getParameter("context"));
        price.setNums(Integer.parseInt(params.getParameter("nums")));
        try{
           return otherService.createLuckyDrawPrice(price);
        }catch (Exception e){
            e.printStackTrace();
            return new SimpleJson(1,"出现未知问题，请排查服务器日志");
        }
    }

    /**
     * 获取最新抽奖活动的奖品及其数量
     * @return 奖品及其数量
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getLuckDrawPrice")
    public SimpleJson getLuckDrawPrice(){
        return new SimpleJson(0,"获取成功",otherService.getLuckDrawPriceLastById());
    }

    /**
     * 获取最新抽奖活动的奖品及其数量管理端
     * @return 奖品及其数量
     */
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getLuckDrawPriceManager")
    public SimpleJson getLuckDrawPriceManager(){
        return new SimpleJson(0,"获取成功",otherService.getLuckDrawPriceLastByIdManager());
    }

    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/joinLuckyDraw")
    public SimpleJson joinLuckyDraw(){
        String luckyState = redisCache.getCacheObject(OPEN_THE_LUCKY_DRAW);
        if(luckyState==null){
            return new SimpleJson(1,"奖励活动状态异常");
        }
        if(!"是".equals(luckyState)){
            return new SimpleJson(1,"抽奖活动暂未开启");
        }
        SapLuckyDrawUser joinLuckyDraw = otherService.isJoinLuckyDraw();
        if(joinLuckyDraw!=null){
            return new SimpleJson(2,"已经加入抽奖活动");
        }
        try{
            otherService.joinLuckyDrawByUsername();
        }catch (Exception e){
            e.printStackTrace();
            return new SimpleJson(1,"加入抽奖活动失败");
        }
        return new SimpleJson(0,"加入抽奖活动成功");
    }

    /**
     * 随机抽奖
     * @return
     */
    @PreAuthorize("hasAuthority('会长')")
    @GetMapping("/randomLuckyDraws")
    public SimpleJson randomLuckyDraws(@RequestParam("luckyDrawThemePriceId")Long luckyDrawThemePriceId){
        List<SapLuckyDrawUser> sapLuckyDrawUsers=null;
        try{
            sapLuckyDrawUsers  = otherService.randomLuckyDraws(luckyDrawThemePriceId);
            if(sapLuckyDrawUsers==null){
                return new SimpleJson(1,"所有报名成员已经全部获奖或奖品已经全部发完");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new SimpleJson(0,sapLuckyDrawUsers);
    }

    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/getAwardInformation")
    public SimpleJson getAwardInformation(){
        return new SimpleJson(0, otherService.getAwardInformation());
    }

    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/isOpenLuckyDraw")
    public SimpleJson isOpenLuckyDraw(){
        String luckyState = redisCache.getCacheObject(OPEN_THE_LUCKY_DRAW);
        if(luckyState==null){
            return new SimpleJson(2,"未知状态");
        }
        if("是".equals(luckyState)){
            return new SimpleJson(0,"已开启");
        }else {
            return new SimpleJson(1,"未开启");
        }
    }
    @PreAuthorize("hasAuthority('游客')")
    @GetMapping("/isJoinLuckyDraw")
    public SimpleJson isJoinLuckyDraw(){
        SapLuckyDrawUser joinLuckyDraw = otherService.isJoinLuckyDraw();
        if(joinLuckyDraw==null){
            return new SimpleJson(0,"未加入抽奖活动");
        }else {
            return new SimpleJson(1,"已经加入抽奖活动");
        }
    }

}
